
package com.azt.front.action.company.shop;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.azt.api.dto.item.ProductExt;
import com.azt.api.dto.item.SkuModel;
import com.azt.api.dto.shop.ProSearchParam;
import com.azt.api.pojo.*;
import com.azt.api.service.CompanyService;
import com.azt.api.service.CompanySignService;
import com.azt.api.service.ProCategoryService;
import com.azt.api.service.ProductService;
import com.azt.front.utils.ContextUtil;
import com.azt.model.AjaxJson;
import com.azt.model.page.TPagination;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 商品页
 * Created by 张栋 on 2017/2/2018:24
 */
@Controller
@RequestMapping("/offer")
public class OfferAction {
    @Autowired
    private CompanyService companyService;


    @Autowired
    private ProductService productService;


    @Autowired
    private CompanySignService companySignService;

    /**
     * 商品详情
     * Created by 张栋 on 2017/2/20 18:27
     */
    @RequestMapping(value = {"/{productid}.html","/{productid}"})
    public String shopOffer(@PathVariable Integer productid, ModelMap model){
        //当前位置
        ProductExt product = productService.getProductExtById(productid);
        if(product==null){
            return "redirect:/selloffer";
        }


        //店铺信息
        Integer customerId = product.getCustomerId();

        //判断公司是否存在
        Company company = companyService.getCompanyById(customerId);

        //获取公司详情
        CompanyInfo info = companyService.getCompanyInfoByCompanyId(customerId);

        //判断店铺是否存在
        CompanyExt shop = companyService.getCompanyExtByCustomerid(customerId);

        if(shop==null){
            return "redirect:/selloffer";
        }
        //热销产品

        //获取热销
        ProSearchParam proSearchParam = new ProSearchParam();
        proSearchParam.setCompanyId(company.getId());
        proSearchParam.setPageNo(1);
        proSearchParam.setPageSize(3);
        proSearchParam.setOrderType(6);//浏览量desc
        TPagination hots = productService.getProductList(proSearchParam);

        //sku属性
        List<ProSku> skugroup= productService.getProSkuByProductId(productid);


        if(skugroup.size()==1 && StringUtils.isBlank(skugroup.get(0).getSku())){
            model.put("nosku",true);
        }

//        List<ProSkuExt> skugroup =   productService.getProSkuExtByProductId(27500);
        List<SkuModel> modes =   productService.geteProModelByProductId(productid);

        model.put("compnay",company);
        model.put("info",info);
        model.put("hots",hots.getList());
        model.put("product",product);
        model.put("shop",shop);
        model.put("models",modes);
        int signboolean=0;
        Company mycompany = ContextUtil.getCompany();
        if(mycompany!=null){//不同用户控制对应的价格
            //'身份(1:金牌采购商,2:金牌服务商,3:主控股东,4:普通股东,5:战略合作商,6:普通会员)',
            //1.银牌采购商,2.金牌服务商,
            CompanySign companySign = companySignService.getCompanySign(mycompany.getId(), 2, true);
//            Integer identity = companySign.getIdentity();
            //金牌服务商、银牌采购商、普通股东突出集采价，
            if(companySign!=null && companySign.getIdentity()!=null ){
                /* Integer identity = companySign.getIdentity();
                if(identity==1 || identity==2 || identity==4){
                    model.put("signboolean",1);
                }
                */

                signboolean=1;
            }
        }
        model.put("signboolean",signboolean);
        JSONArray  array = new JSONArray();
        JSONObject  skumap = new JSONObject();

        for (ProSku proSku : skugroup) {
            array.add(proSku.getSku()==null?"":proSku.getSku());
            JSONObject obj = new JSONObject();
            obj.put("price",proSku.getPrice());
            obj.put("id",proSku.getId());
            obj.put("hotPrice",proSku.getHotPrice());
            obj.put("memberPrice",proSku.getMemberPrice());
            if(signboolean==1){
                obj.put("cenPrice",proSku.getCenPrice());
            }else{
                obj.put("cenPrice",0);
            }
            obj.put("stockQuantity",proSku.getStockQuantity());
            skumap.put(proSku.getSku()==null?"":proSku.getSku(), obj);
        }


        model.put("skumap",skumap.toString());
        model.put("skugroup", array.toString());
        model.put("skumodels", net.sf.json.JSONArray.fromObject(modes).toString());





        //访问次数
        productService.addHitTimes(product.getId());

        AjaxJson j = validPermission(product.getId());
        if(!j.isSuccess()){
            model.put("msg",j.getMsg());
            model.put("code",j.getCode());
        }

        return "shop/offer";
    }



    /**
     * 购买权限
     * Created by 张栋 on 2017/3/21 9:55
     */
    @RequestMapping("/permission")
    @ResponseBody
    public AjaxJson validPermission(Integer productid){
        AjaxJson j = new AjaxJson();
        ProductExt product = productService.getProductExtById(productid);
        User user = ContextUtil.getUser();
        Company mycompany = ContextUtil.getCompany();

        if(mycompany!=null){
     /*       Integer purchase = mycompany.getIspurchaser();
            Integer provider = mycompany.getIsprovider();
            if(purchase!=1 && provider==1){
                j.setCode(3);
                j.setSuccess(false);
                j.setMsg("您是供应商角色，无法购买商品");
            }
    */

            if(product.getCustomerId().equals(mycompany.getId())){
                j.setSuccess(false);
                j.setCode(2);
                j.setMsg("无法购买您自己公司的商品。");
            }

            if(mycompany.getAuditState()!=3){
                j.setSuccess(false);
                j.setCode(6);
                j.setMsg("公司资料未审核通过,无法购买。");
            }


        }else{
            if(user!=null){
                j.setCode(4);
                j.setSuccess(false);
                j.setMsg("您尚未完善公司资料，无法购买");
            }
        }

        if(product.getIfPub()!=1 ){
            j.setSuccess(false);
            j.setCode(1);
            j.setMsg("该商品已下架");
        }

        if(product.getRemoved()==1){
            j.setSuccess(false);
            j.setCode(5);
            j.setMsg("该商品已删除");
        }

        if(product.getRemoved()==1){
            j.setSuccess(false);
            j.setCode(7);
            j.setMsg("该商品已经删除,无法购买。");
        }

        if(product.getState()!=1){
            j.setSuccess(false);
            j.setCode(8);
            j.setMsg("该商品还未通过审核,无法购买。");
        }

        return j;
    }

}
